/**
 * Extracts a complete JSON object from a text string
 * This function is directly based on the original script's implementation
 * @param {string} text - Text containing a JSON object
 * @returns {string|null} - Complete JSON string or null if not found
 */
export function extractCompleteJSON(text) {
  if (!text) return null;
  
  const firstBrace = text.indexOf('{');
  if (firstBrace === -1) return null;

  let braceCount = 0;
  let jsonStr = '';
  let started = false;

  for (let i = 0; i < text.length; i++) {
    const char = text[i];

    if (char === '{' && !started) {
      started = true;
      braceCount = 1;
      jsonStr += char;
    } else if (started) {
      jsonStr += char;

      if (char === '{') {
        braceCount++;
      } else if (char === '}') {
        braceCount--;

        if (braceCount === 0) {
          // Found matching closing brace
          return jsonStr;
        }
      }
    }
  }

  // If no complete JSON found
  return null;
} 